﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Pansoft.HIME.Manager;
using ActionFilterAttribute = System.Web.Http.Filters.ActionFilterAttribute;

namespace Pansoft.HIME.Controllers.Filter
{
    public class ExecutionTimingAttribute : ActionFilterAttribute
    {
        private readonly bool _TimingEnabled = Options.Instance.PageTimingEnabled;
        private Stopwatch _Stopwatch;

        public override void OnActionExecuting(HttpActionContext context)
        {
            base.OnActionExecuting(context);
            if (_TimingEnabled)
            {
                _Stopwatch = new Stopwatch();
                _Stopwatch.Start();
            }
        }

        public override void OnActionExecuted(HttpActionExecutedContext context)
        {
            base.OnActionExecuted(context);
            if (_TimingEnabled)
            {
                _Stopwatch.Stop();
                //TODO:时间算出来咋个扔出去还没有想好
                Console.WriteLine(_Stopwatch.ElapsedMilliseconds);
            }
        }
    }
}